<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_seminar extends CI_Model {
   
        function __construct(){
        parent::__construct();
    }
       
        function getdata($plimit=true){
                # get parameter from easy grid
                $page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
                $limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
                $sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'id';  
                $order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
                $offset = ($page-1)*$limit;
               
                #get filter
                $kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
                $kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
                $tahun = isset($_POST['tahun']) ? strval($_POST['tahun']) : '';
               
                # create query
                $this->db->flush_cache();
                $this->db->start_cache();
                $this->db->select('a.*,DATE_FORMAT(a.tgl_seminar1, \'%d-%m-%Y\') tgl_seminar2', false);
                $this->db->select('m.nama');
                $this->db->from('tbl_seminar a');
                $this->db->join('tbl_mahasiswa as m','m.nim= a.nim');
                $this->db->join('tbl_up as u','u.nim= a.nim and u.tahun_id=a.tahun_id');
                $this->db->join('ref_prodi p', 'p.kd_prodi = m.kd_prodi');
               
                       
                $this->db->where('p.kd_fakultas', $kd_fakultas);
                $this->db->where('m.kd_prodi', $kd_prodi);
                $this->db->where('a.tahun_id', $tahun);
                $this->db->where('u.proposal', "Lulus");
                $this->db->where('u.status', "A");
                $this->db->where('a.status', "A");
                
                       
                $this->db->order_by($sort, $order);
                $this->db->stop_cache();
               
                # get count
                $tmp['row_count'] = $this->db->get()->num_rows();
               
                # get data
                if($plimit == true){
                        $this->db->limit($limit, $offset);
                }
                $tmp['row_data'] = $this->db->get();
               
                return $tmp;
        }
       
        function getdataedit($kode){
                $this->db->flush_cache();
                $this->db->select('a.*');
                $this->db->select('p.kd_fakultas, p.kd_prodi');
                $this->db->select('m.nama');
                $this->db->from('tbl_seminar a');
                $this->db->join('tbl_mahasiswa as m','m.nim= a.nim');
                $this->db->join('ref_prodi p', 'p.kd_prodi = m.kd_prodi');
                
                $this->db->where('a.id', $kode);
               
                return $this->db->get();
        }
       
        function togrid($data, $count){
                $response->total = $count;
                if($count>0){
                        $i=0;
                        foreach($data->result_array() as $row){
                                foreach($row as $key => $value){
                                        $response->rows[$i][$key]=$value;
                                }
                                $i++;
                        }
                }else{
					$response->rows = array();
				}
                return json_encode($response);
        }
       
        function isExistKode($kode){
                //if ($kode!=null)
                        //$this->db->where('nim',$kode);
				$this->db->where('nim', $kode['nim']);
				$this->db->where('tahun_id', $kode['tahun_id']);
				$this->db->where('status', 'A');
			   
                $this->db->select('*');
                $this->db->from('tbl_seminar');
                $query = $this->db->get();
               
                $rs = $query->num_rows() ;              
                $query->free_result();
               
                return ($rs>0);
        }
       
        function InsertOnDb($data){
                //query insert data            
                $this->db->flush_cache();
                $this->db->set('id', $data['id']);
                $this->db->set('tahun_id', $data['tahun_id']);
                $this->db->set('nim', $data['nim']);
                $this->db->set('judul_ta', $data['judul_ta']);
                $this->db->set('abstrak', $data['abstrak']);
                $this->db->set('pembimbing1', $data['pembimbing1']);
                $this->db->set('pembimbing2', $data['pembimbing2']);
                //$this->db->set('seminar', $data['seminar']);
                $this->db->set('keterangan', $data['keterangan']);
                //$this->db->set('tgl_daftar',date('Y-m-d'));
                $this->db->set('tgl_seminar1',FormatDateToMysql($data['tgl_seminar1']));
               //	$this->db->set('tgl_selesai', date('Y-m-d', strtotime("+6 months", strtotime(FormatDateToMysql($data['tgl_mulai'])))));
				if($data['file_upload'] != ''){
				$this->db->set('file_proposal', $data['file_upload']);
				}
				
                $this->db->set('CUID', $this->session->userdata('user_id'));
				$this->db->set('CDATE', date('Y-m-d H:i:s'));
				
                
                $result = $this->db->insert('tbl_seminar');
               
                //return
                if($result) {
                        return TRUE;
                }else {
                        return FALSE;
                }
        }
       
        function UpdateOnDb($data){
                //query edit data              
                $this->db->flush_cache();
				
                $this->db->set('judul_ta', $data['judul_ta']);
                $this->db->set('abstrak', $data['abstrak']);
                $this->db->set('pembimbing1', $data['pembimbing1']);
                $this->db->set('pembimbing2', $data['pembimbing2']);
                //$this->db->set('seminar', $data['seminar']);
                $this->db->set('keterangan', $data['keterangan']);
                $this->db->set('tgl_seminar1',FormatDateToMysql($data['tgl_seminar1']));
               // $this->db->set('tgl_selesai', date('Y-m-d', strtotime("+6 months", strtotime(FormatDateToMysql($data['tgl_mulai'])))));
				
				$this->db->set('MUID', $this->session->userdata('user_id'));
				$this->db->set('MDATE', date('Y-m-d H:i:s'));
				
				$this->db->where('id', $data['id']);
				$result = $this->db->update('tbl_seminar');
               
			   
                //return
                if($result) {
                        return TRUE;
                }else {
                        return FALSE;
                }
        }
        function DeleteOnDb($data){
                //query delet data  dengan ganti status db            
                $this->db->flush_cache();
				
                $this->db->set('status', "D");
               
                $this->db->set('MUID', $this->session->userdata('user_id'));
				$this->db->set('MDATE', date('Y-m-d H:i:s'));
				
				$this->db->where('id', $data);
				$result = $this->db->update('tbl_seminar');
               
			   
                //return
                if($result) {
                        return TRUE;
                }else {
                        return FALSE;
                }
        }
		
		
        /*function DeleteOnDb($kode){
		 //query delet data di db
                $this->db->flush_cache();
                $this->db->where('id', $kode);
                $result = $this->db->delete('tbl_up');
               
                if($result) {
                        return TRUE;
                }else {
                        return FALSE;
                }
        }*/
       
}

?>

